package com.changgou.service.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查找所有品牌
     * @return
     */
    @GetMapping
    public Result<List<Brand>> findAll(){
        return new Result<>(true, StatusCode.OK,"查询成功",brandService.findAll());
    }

    /**
     * 根据查找单个品牌
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        return new Result(true,StatusCode.OK,"查找单个品牌成功",brandService.findById(id));
    }

    /**
     * 添加品牌
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加品牌成功");
    }

    /**
     * 删除品牌
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 修改品牌
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 条件查询
     * @return
     */
    @GetMapping("/search")
    public Result findList(@RequestParam Map<String,Object> map){
        return new Result(true,StatusCode.OK,"条件查询",brandService.findList(map));
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result findPage(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<Brand> page1 = brandService.findPage(page, size);
        PageResult<Brand> brandPageResult = new PageResult<>(page1.getTotal(), page1.getResult());
        return new Result(true,StatusCode.OK,"分页查询",brandPageResult);
    }

    /**
     * 分页条件查询
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result findListAndFindPage(@PathVariable("page")
                                                  Integer page,@PathVariable("size") Integer size,@RequestParam Map<String,Object> map){
        Page<Brand> listAndfindPage = brandService.findListAndfindPage(map, page, size);
        PageResult<Brand> brandPageResult = new PageResult<Brand>(listAndfindPage.getTotal(), listAndfindPage.getResult());
        return new Result(true,StatusCode.OK,"分页条件查询",brandPageResult);
    }
}
